package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/*
练习，给教语文的老师工资加500
试试事务提交与回滚
 */
public class Test {
    public static void main(String[] args) {
        try (Connection conn=DBUtil.getConnection();){
            /*
            UPDATE teacher t,subject sub SET t.salary=t.salary+500
            WHERE  sub.id=t.subject_id
            AND sub.name='语文';
             */
            conn.setAutoCommit(false);
            String s="UPDATE teacher t,subject sub SET t.salary=t.salary+500\n" +
                    "WHERE  sub.id=t.subject_id\n" +
                    "AND sub.name='语文';";
            Statement state= conn.createStatement();
            int a=state.executeUpdate(s);
            System.out.println(a>0?"修改成功":"修改失败");
            /*
            conn.commit();
            System.out.println("事务提交了！");
             */

            conn.rollback();
            System.out.println("事务回滚了！");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
